package uid.elements;

import uid.jsf.Column;
import uid.jsf.Componente;
import uid.jsf.DataTable;
import uid.jsf.InputText;
import uid.jsf.OutputLabel;
import uid.jsf.OutputText;
import uid.jsf.PanelGrid;

/**
 * @author Patricia Vilain
 */
public class InputStructure extends Structure {

	private Componente componente;

	public InputStructure(String name) {
		super(name);
	}

	/**
	 * Insere o item de dado na estrutura com nome nameStructure, procurando
	 * tambem as estruturas contidas nela.
	 */
	public boolean insertInputDataItemInStructure(String nameStructure,
			InputDataItem dataItem) {
		if (this.name.equals(nameStructure)) {
			for (int i = 0; i < this.listDataItems.size(); i++) {
				UIDElement elem = this.listDataItems.get(i);
				if (elem instanceof InputDataItem
						&& ((InputDataItem) elem).getName().equals(
								dataItem.getName()))
					return false;
			}
			this.listDataItems.add(dataItem);
			return true;
		} else {
			for (int i = 0; i < this.listStructures.size(); i++) {
				UIDElement elem = this.listStructures.get(i);
				if (elem instanceof InputStructure) {
					boolean insertionOk = ((InputStructure) elem)
							.insertInputDataItemInStructure(nameStructure,
									dataItem);
					if (insertionOk)
						return true;
				}
			}
			return false;
		}
	}

	/**
	 * Remove o item de dado da estrutura com nome nameStructure, procurando
	 * tambem as estruturas contidas nela.
	 */
	public boolean removeInputDataItemFromStructure(String nameStructure,
			String nameDataItem) {
		if (this.name.equals(nameStructure)) {
			for (int i = 0; i < this.listDataItems.size(); i++) {
				UIDElement elem = this.listDataItems.get(i);
				if (elem instanceof InputDataItem
						&& ((InputDataItem) elem).getName()
								.equals(nameDataItem)) {
					this.listDataItems.remove(i);
					return true;
				}
			}
			return false;
		} else {
			for (int i = 0; i < this.listStructures.size(); i++) {
				UIDElement elem = this.listStructures.get(i);
				if (elem instanceof InputStructure) {
					boolean insertionOk = ((InputStructure) elem)
							.removeInputDataItemFromStructure(nameStructure,
									nameDataItem);
					if (insertionOk)
						return true;
				}
			}
			return false;
		}
	}

	/**
	 * Insere a estrutura na estrutura com nome nameStructure, procurando tambem
	 * as estruturas contidas nela.
	 */
	public boolean insertInputStructureInStructure(String nameStructure,
			InputStructure structure) {
		if (this.name.equals(nameStructure)) {
			for (int i = 0; i < this.listStructures.size(); i++) {
				UIDElement elem = this.listStructures.get(i);
				if (elem instanceof InputStructure
						&& ((InputStructure) elem).getName().equals(
								structure.getName()))
					return false;
			}
			this.listStructures.add(structure);
			return true;
		} else {
			for (int i = 0; i < this.listStructures.size(); i++) {
				UIDElement elem = this.listStructures.get(i);
				if (elem instanceof InputStructure) {
					boolean insertionOk = ((InputStructure) elem)
							.insertInputStructureInStructure(nameStructure,
									structure);
					if (insertionOk)
						return true;
				}
			}
			return false;
		}
	}

	/**
	 * Remove a estrutura estrutura com nome nameStructure, procurando tambem as
	 * estruturas contidas nela.
	 */
	public boolean removeInputStructureFromStructure(String nameStructure,
			String nameStructureToBeExcluded) {
		if (this.name.equals(nameStructure)) {
			for (int i = 0; i < this.listStructures.size(); i++) {
				UIDElement elem = this.listStructures.get(i);
				if (elem instanceof InputStructure
						&& ((InputDataItem) elem).getName().equals(
								nameStructureToBeExcluded)) {
					this.listStructures.remove(i);
					return true;
				}
			}
			return false;
		} else {
			for (int i = 0; i < this.listStructures.size(); i++) {
				UIDElement elem = this.listStructures.get(i);
				if (elem instanceof InputStructure) {
					boolean insertionOk = ((InputStructure) elem)
							.removeInputStructureFromStructure(nameStructure,
									nameStructureToBeExcluded);
					if (insertionOk)
						return true;
				}
			}
			return false;
		}
	}

	public boolean insertInputOptionalDataItemInStructure(String nameStructure,
			InputOptionalDataItem dataItem) {
		if (this.name.equals(nameStructure)) {
			for (int i = 0; i < this.listDataItems.size(); i++) {
				UIDElement elem = this.listDataItems.get(i);
				if (elem instanceof InputDataItem
						&& ((InputDataItem) elem).getName().equals(
								dataItem.getName()))
					return false;
			}
			this.listDataItems.add(dataItem);
			return true;
		} else {
			for (int i = 0; i < this.listStructures.size(); i++) {
				UIDElement elem = this.listStructures.get(i);
				if (elem instanceof InputStructure) {
					boolean insertionOk = ((InputStructure) elem)
							.insertInputOptionalDataItemInStructure(
									nameStructure, dataItem);
					if (insertionOk)
						return true;
				}
			}
			return false;
		}
	}
}
